import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import {createPinia} from 'pinia'
import './api/mock.js'
import api from './api/api.js'

import { useStore } from './store'

// const pinia = createPinia()
const app = createApp(App)
// useStore(app)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component) 
  }

app.use(router)
app.use(createPinia())

app.config.globalProperties.$api = api
declare module '@vue/runtime-core'{
    export interface ComponentCustomProperties{
        $api:Object
    }
}

const test = useStore()
// test.addMenu(router)
// console.log(router);
// 路由守卫
router.beforeEach((to,from,next)=>{
    test.getToken()
    const token = test.token
    if(!token&&to.name!=='login'){
        next({name:"login"})
    }else if(!router.hasRoute(to.name)){
        next({name:"home"})
    }else{
        next()
    }
})

app.mount('#app')
